package com.leadzone.websocket;

import java.io.IOException;
import java.nio.ByteBuffer;

import javax.websocket.OnMessage;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

//通过注释的方式
@ServerEndpoint("/socket/echo")
public class AnnotationSokcet {
	  @OnMessage
	    public void echoTextMessage(Session session, String msg, boolean last) {
	        try {
	            if (session.isOpen()) {
	               System.out.println(msg);
	                session.getBasicRemote().sendText(msg, last);
	            }
	        } catch (IOException e) {
	            try {
	                session.close();
	            } catch (IOException e1) {
	                // Ignore
	            }
	        }
	    }

	    @OnMessage
	    public void echoBinaryMessage(Session session, ByteBuffer bb,
	            boolean last) {
	        try {
	            if (session.isOpen()) {
	                System.out.println(bb);
	                session.getBasicRemote().sendBinary(bb, last);
	            }
	        } catch (IOException e) {
	            try {
	                session.close();
	            } catch (IOException e1) {
	                // Ignore
	            }
	        }
	    }

	    /**
	     * Process a received pong. This is a NO-OP.
	     *
	     * @param pm    Ignored.
	     */
	    @OnMessage
	    public void echoPongMessage(PongMessage pm) {
	        // NO-OP
	    }

}
